package advance.reflection;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * 通过反射调用方法
 */
public class ReflectionDemo04 {

    public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        A a = new A();
        Class c = a.getClass();
        //c.getDeclaredMethod("print", int.class, int.class);
        Method m = c.getDeclaredMethod("print", new Class[]{int.class, int.class});
        //m.invoke(a, 10, 20);
        m.invoke(a, new Object[]{10, 20});
        System.out.println("===============");
        Method m2 = c.getDeclaredMethod("print", new Class[]{String.class, String.class});
        m2.invoke(a, new Object[]{"hello", "WORLD"});
        System.out.println("===============");
        Method m3 = c.getDeclaredMethod("print");
        m3.invoke(a);
    }
}

class A {

    public void print() {
        System.out.println("empty constructor ...");
    }

    public void print(int a, int b) {
        System.out.println(a + b);
    }

    public void print(String a, String b) {
        System.out.println(a.toUpperCase() + "," + b.toLowerCase());
    }
}